package com.common.DAO;

import java.util.List;

import com.common.entity.Registration;

public class RegistrationDAO  extends BaseDAO implements IRegistrationDAO {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Registration> list() {
		System.out.println("IN DAO");
   	  	System.out.println("\n --- \n");
   	  	try{
   	  	List<Registration> test = super.list("from Registration");
   	  	System.out.println(test.size());
   	  	return test;
   	  	}catch (Exception e) {
			logger.error("erreur requete",e);
		}
   	  return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Registration> listRegistrationByCovoitureurId(int id_covoit){
		logger.info("listRegistrationByCovoitureurId()");
		try{
			List<Registration> listR = super.list("from Registration where fk_covoitureur="+id_covoit);
			System.out.println(listR.size());
			logger.info("listRegistrationByCovoitureurId() done");		
	   	  	return listR;
	   	  }catch (Exception e) {
				logger.error("erreur requete",e);
		}
		logger.info("listRegistrationByCovoitureurId() done");		
		return null;
	}
	
	@Override
	public List<Registration> listRegistrationByPathId(int id_path){
		logger.info("listRegistrationByCovoitureurId()");
		try{
			List<Registration> listR = super.list("from Registration where id_path="+id_path);
			System.out.println("Listregistration size : "+listR.size());
			logger.info("listRegistrationByPathId() done");		
	   	  	return listR;
	   	  }catch (Exception e) {
				logger.error("erreur requete",e);
		}
		return null;
	}
	
	
	@Override
	public void addRegistration(Registration registration) {
		logger.info("AddRegistration()");
		try{
			super.save(registration);
		}catch (Exception e) {
			logger.error("erreur in DAO while adding a registration");
		}
		logger.info("addRegistration() done");		
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isCovoitureurRegisterForPath(int id_covoitureur, int id_path) {
		logger.info("isCovoitureurRegisterForPath()");
		try{
			List<Registration> listR = super.list("from Registration where fk_covoitureur="+id_covoitureur+" and fk_trajet="+id_path);
			logger.info("listRegistrationByCovoitureurId() done");	
			if(listR.size()==0) return false;
			else return true;
	   	  }catch (Exception e) {
				logger.error("erreur requete",e);
		}
		logger.info("isCovoitureurRegisterForPath() done");		
		return false;
	}

	@Override
	public void removeRegistration(Registration registration) {
		logger.info("removeRegistration()");
		try{
			super.remove(registration);
		}catch (Exception e) {
			logger.error("erreur in DAO while removing a registration");
		}
		logger.info("removeRegistration() done");	
	}
}
